import { Component, OnInit } from '@angular/core';
import { NavController } from 'ionic-angular';
import { ListPage } from '../list/list.component';
import { HomeService } from './home.service';
import { DetailPage } from '../detail/detail.component';

@Component({
  selector: 'page-home',
  templateUrl: 'home.component.html',
  providers: [HomeService]
})
export class HomePage implements OnInit {
  tasks: Array<any> = [];

  constructor(
    public navCtrl: NavController,
    public homeService: HomeService) {
  }

  ngOnInit() {
    this.homeService.listProcurementPlan().subscribe(data => {
      this.tasks = data['tasks'];
      this.tasks.forEach(ele => {
        if(ele.materialApplies.length == 0){
          if(ele.finishFlag == 1){
            ele['status'] = -1;//已结束
          }else{
            ele['status'] = 0;//可申请
          }
        }else{
          if(ele.taskStateID == 1)ele['status'] = 1;//已申请
          if(ele.taskStateID == 2)ele['status'] = 2;//审批中
          if(ele.taskStateID == 3)ele['status'] = 3;//不通过
          if(ele.taskStateID == 4)ele['status'] = 4;//审批通过
          if(ele.taskStateID == 5)ele['status'] = 5;//采购中
          if(ele.taskStateID == 6)ele['status'] = 6;//采购完成、待领取
          if(ele.taskStateID == 7)ele['status'] = 7;//已领取
        }
      });
    });
  }

  goToList(taskID) {
    this.navCtrl.push(ListPage, {'taskID': taskID});
  }

  goToDetail(task) {
    this.navCtrl.push(DetailPage, {'task': task,'apply':task.materialApplies[0]});
  }

  recieve(taskID){
    console.log("更新已领取状态");
  }
}
